﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_RepaymentPlanItem")]
    public class P_RepaymentPlanItem : IEntity<P_RepaymentPlanItem>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string UserName { get; set; }
        public int PlanID { get; set; }
        public int CycleNumber { get; set; }
        public DateTime RepaymentDate { get; set; }
        public decimal RepaymentInterest { get; set; }
        public decimal RepaymentPrincipal { get; set; }
        public decimal ReMainPrincipal { get; set; }
        public decimal ReMainInterest { get; set; }
        public int Status { get; set; }
        public DateTime RepaymentTime { get; set; }
        public int RepaymentType { get; set; }
        public int AmountOperationID { get; set; }
        public int ProjectId { get; set; }
        public int PolicyId { get; set; }

        public override P_RepaymentPlanItem Find(string key)
        {
            int Id = Int32.Parse(key);
            return Find(c => c.ID == Id);
        }

        public override P_RepaymentPlanItem Find(System.Linq.Expressions.Expression<Func<P_RepaymentPlanItem, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.RepaymentPlanItem.FirstOrDefault(where);
                if (item != null)
                {
                    this.AmountOperationID = item.AmountOperationID;
                    this.CycleNumber = item.CycleNumber;
                    this.ID = item.ID;
                    this.PlanID = item.PlanID;
                    this.RepaymentDate = item.RepaymentDate;
                    this.RepaymentInterest = item.RepaymentInterest;
                    this.RepaymentPrincipal = item.RepaymentPrincipal;
                    this.ReMainPrincipal = item.ReMainPrincipal;
                    this.ReMainInterest = item.ReMainInterest;
                    this.RepaymentTime = item.RepaymentTime;
                    this.RepaymentType = item.RepaymentType;
                    this.Status = item.Status;
                    this.UserName = item.UserName;
                    this.PolicyId = item.PolicyId;
                    this.ProjectId = item.ProjectId;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_RepaymentPlanItem, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.RepaymentPlanItem.Any(where);
            }
        }
    }
}